<?php
define('MIN_WITHDRAW_AMOUNT',100);
/** 
 * 用户收益提现
 * 
 * @property WithdrawOrderModel $WithdrawModel
 * @property WxTransfers $transfersAPI
 * 
 */
class Transfers extends Bg_Controller {
	
	public function __construct() {
		parent::__construct();
// 		$this->load->database();
		$this->load->model('User_mdl','user');
		$this->load->model('WithdrawOrderModel','WithdrawModel');
		$this->load->library('wxApi/WxTransfers',NULL,'transfersAPI');
	}
	
	/**
	 * 未转帐的订单列表
	 */
	private $unTransferOrders = array();
	private $userMap = array();
	
	public function run(){
		
		$this->loadUnTransferOrders();
		foreach ($this->unTransferOrders as $order){
			$this->transfer($order);
		}
// 		pr($this->db->queries);
		
	}
	
	private function loadUnTransferOrders(){
		$where = array('status'=>WithdrawStatus::UNPAY,'wx_openid !='=>NULL);
		$select = '*';
		$list = $this->WithdrawModel->getList($select,$where);
		$this->unTransferOrders = $list;
	}
	
	private function transfer($order){
		
		$orderId = $order['id'];
		$uid = $order['user_id'];
		$openID = $order['wx_openid'];
		$amount = $order['amount'];
		$desc = !empty($order['desc'])?$order['desc']:'荣钧健康饮水';
		$userSelect = 'id,phone';
		$user = $this->user->getOneById($uid,$userSelect);
		if(empty($user)){
			return FALSE;
		}

		if( $amount<MIN_WITHDRAW_AMOUNT ){
			$wxError = '';
			$failReason = '最小提现金额为:'.(MIN_WITHDRAW_AMOUNT/100).'元';
			$this->WithdrawModel->transfersFail($orderId, $uid, $amount, $wxError, $failReason);
			return FALSE;
		}
		
		$wxLog = '';
		// 调用转账
		try {
// 			$openID = $user['wxgzopenid'];
			$tradeID = 'hs'.$orderId;
			$this->transfersAPI->payFrom('yx');//调用公众号支付
			$result = $this->transfersAPI->transfres($openID, $amount, $tradeID,$desc);
			$wxLog = $this->transfersAPI->getLog();
			
			if(!empty($result['payment_no'])){
				$wxPaymentID = $result['payment_no'];
				$this->WithdrawModel->transfersDone($orderId, $uid, $amount, $wxPaymentID,$wxLog);
			}else{
				$wxError = '';
				if(!empty($result['err_code'])){
					$wxError = $result['err_code_des'];
				}else if(!empty($result['err_code_des'])){
					$wxError = $result['err_code_des'];
				}else if(!empty($result['return_msg'])){
					$wxError = $result['return_msg'];
				}
				
				$failReason = '转账失败';
				$this->WithdrawModel->transfersFail($orderId, $uid, $amount, $wxError, $failReason,$wxLog);
			}
		} catch (Exception $e) {
			
			$wxError = $e->getMessage();
			$failReason = '转账失败';
			$wxLog = $this->transfersAPI->getLog();
			$this->WithdrawModel->transfersFail($orderId, $uid, $amount, $wxError, $failReason,$wxLog);
		}
	}
	
	
	public function test(){
			$openid = 'oar3QwcinAE-QNKi3lK-ySH3P9b4';
			$amount = 100;
			$partner_trade_no = uniqid();
			$desc = 'test';
			$result = $this->transfersAPI->transfres($openid, $amount, $partner_trade_no,$desc);
			var_dump($result);
	}
	
	public function test2(){
		$data = array(
				'user_id'=>41,
				'amount'=>112,
		);
		$res = $this->WithdrawModel->user_withdraw($data);
		var_dump($res);
		pr($this->db->queries);
	}
	
}

